package com.lwj.yuangong2.controller;

import com.lwj.yuangong2.entity.Emp;
import com.lwj.yuangong2.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/emp")
@CrossOrigin
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @Value("${photo.dir}")
    private String realPath;

    /**
     * 查询员工
     * @return
     */
    @GetMapping("/findAll")
    public List<Emp> findAll(){
        return empService.findAll();
    }

    /**
     * 添加员工
     */
    @PostMapping("/addEmp")
    public Map<String,Object> addEmp(Emp emp, MultipartFile photo) throws IOException {
        Map<String,Object> map = new HashMap<>();
        try {
            //上传头像
            //修改头像的文件名
            String newFileName = UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath,newFileName));
            //设置头像地址
            emp.setPath(newFileName);
            //调用业务层
            empService.addEmp(emp);
            //给map设置
            map.put("state",true);
            map.put("msg","员工信息添加成功！");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            map.put("state",false);
            map.put("msg","员工信息添加失败");
        }
        return  map;
    }

    /**
     * 删除员工信息
     */
    @GetMapping("/delete")
    public Map<String,Object> delete(String id){
        Map<String, Object> map = new HashMap<>();
        try {
            empService.delete(id);
            map.put("state",true);
            map.put("msg","删除员工信息成功!");
        } catch (Exception e) {
            map.put("state",false);
            map.put("msg","删除员工信息失败!");
        }
        return  map;
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("/findById")
    public Emp findById(String id){
        return empService.findById(id);
    }

    /**
     * 修改员工信息
     */
    @PostMapping("/update")
    public Map<String,Object> update(Emp emp, MultipartFile photo) throws IOException {
        Map<String,Object> map = new HashMap<>();
        try {
            if (photo != null&&photo.getSize() != 0){
                //上传头像
                //修改头像的文件名
                String newFileName = UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath,newFileName));
                //设置头像地址
                emp.setPath(newFileName);
            }
            //调用业务层
            empService.update(emp);
            //给map设置
            map.put("state",true);
            map.put("msg","员工信息更新成功！");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            map.put("state",false);
            map.put("msg","员工信息更新失败");
        }
        return  map;
    }
}
